function varargout = Frequency_Parameters(varargin)
% FREQUENCY_PARAMETERS M-file for Frequency_Parameters.fig
%      FREQUENCY_PARAMETERS, by itself, creates a new FREQUENCY_PARAMETERS or raises the existing
%      singleton*.
%
%      H = FREQUENCY_PARAMETERS returns the handle to a new FREQUENCY_PARAMETERS or the handle to
%      the existing singleton*.
%
%      FREQUENCY_PARAMETERS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in FREQUENCY_PARAMETERS.M with the given input arguments.
%
%      FREQUENCY_PARAMETERS('Property','Value',...) creates a new FREQUENCY_PARAMETERS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Frequency_Parameters_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Frequency_Parameters_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Frequency_Parameters

% Last Modified by GUIDE v2.5 25-Apr-2005 15:41:47

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Frequency_Parameters_OpeningFcn, ...
                   'gui_OutputFcn',  @Frequency_Parameters_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before Frequency_Parameters is made visible.
function Frequency_Parameters_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Frequency_Parameters (see VARARGIN)
global Project;
% Choose default command line output for Frequency_Parameters
handles.output = hObject;

% Create frequency resoultion values
BlockSize(1) = 65536;
for index = 2:8
    BlockSize(index) = BlockSize(index - 1) / 2;
end
VFreqRes = Project.Test_Item.Run.Sample_Rate ./ BlockSize;
handles.VFreqRes = VFreqRes;
FreqResStr = num2str(VFreqRes(1), 4);
Delimiter = '|';
for index = 2:8
    TempStr = strcat(Delimiter, num2str(VFreqRes(index), 5));
    FreqResStr = strcat(FreqResStr, TempStr);
end
% Load frequency resoultion values into the Listbox
set(handles.Freq_Resolution, 'string', FreqResStr, ...
                            'min', 1, 'max', 1, 'listboxtop', 5, 'value', 5);

% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Frequency_Parameters wait for user response (see UIRESUME)
uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = Frequency_Parameters_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

status = handles.option;
if strcmp(status, 'OK')
    % Get default command line output from handles structure
    varargout{1} = handles.Freq_Resolution;
    varargout{2} = handles.Low_Freq;
    varargout{3} = handles.Upper_Freq;
else
    varargout{1} = [];
    varargout{2} = [];
    varargout{3} = [];
end
delete(hObject);

% --- Executes during object creation, after setting all properties.
function Freq_Resolution_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Freq_Resolution (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
% Update handles structure
guidata(hObject, handles);

% --- Executes on selection change in Freq_Resolution.
function Freq_Resolution_Callback(hObject, eventdata, handles)
% hObject    handle to Freq_Resolution (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns Freq_Resolution contents as cell array
%        contents{get(hObject,'Value')} returns selected item from Freq_Resolution

% --- Executes during object creation, after setting all properties.
function Lower_Freq_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Lower_Freq (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
% Update handles structure
guidata(hObject, handles);

function Lower_Freq_Callback(hObject, eventdata, handles)
% hObject    handle to Lower_Freq (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Lower_Freq as text
%        str2double(get(hObject,'String')) returns contents of Lower_Freq as a double
% Update handles structure

% --- Executes during object creation, after setting all properties.
function Upper_Freq_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Upper_Freq (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
% Update handles structure
guidata(hObject, handles);

function Upper_Freq_Callback(hObject, eventdata, handles)
% hObject    handle to Upper_Freq (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Upper_Freq as text
%        str2double(get(hObject,'String')) returns contents of Upper_Freq as a double
% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in OK_Button.
function OK_Button_Callback(hObject, eventdata, handles)
% hObject    handle to OK_Button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

Freq_Res_Value = get(handles.Freq_Resolution, 'value');
Freq_Res = handles.VFreqRes(Freq_Res_Value);
LowFreq = str2num(get(handles.Lower_Freq, 'string'));
UpperFreq = str2num(get(handles.Upper_Freq, 'string'));
% Assign results to the GUI output
% handles.output = [Freq_Res LowFreq UpperFreq];
handles.Freq_Resolution = Freq_Res;
handles.Low_Freq = LowFreq;
handles.Upper_Freq = UpperFreq;
handles.option = 'OK';
% Update handles structure
guidata(hObject, handles);
uiresume;

% --- Executes on button press in Cancel_Button.
function Cancel_Button_Callback(hObject, eventdata, handles)
% hObject    handle to Cancel_Button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

handles.option = 'Cancel';
% Close the Modal Dialog box
% delete(handles.figure1);
% Update handles structure
guidata(hObject, handles);
uiresume;
